package guo.dbutil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ConnectionTest {
	public static void main(String[] args) {
		String url="jdbc:oracle:thin:@localhost:1521:orcl";
		String sql="select *from users";
		Connection connection=null;
		//try(connection=DriverManager.getConnection(url, "testdb", "testdb"))
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			connection=DriverManager.getConnection(url, "testdb", "testdb");
			System.out.println("database connection success!");
			PreparedStatement pstm=connection.prepareStatement(sql);
			ResultSet rs=pstm.executeQuery();
			//ORM
			while(rs.next()) {
				System.out.print(rs.getInt(1)+",");
				System.out.print(rs.getString(2)+",");
				System.out.print(rs.getString(3)+",");
				System.out.print(rs.getString(4)+",");
				System.out.println();
			}
		}catch(ClassNotFoundException  e) {
			System.out.println("class not found,error message is"+e.getMessage());
		}catch(SQLException  e){
			System.out.println("SQL not found,error message is"+e.getMessage());
			e.printStackTrace();
			
		}finally {
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
